package com.ydl.shopping.service.impl;

import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydl.shopping.core.entity.TreeSelect;
import com.ydl.shopping.core.enums.ResultEnum;
import com.ydl.shopping.domain.TbRole;
import com.ydl.shopping.domain.TbRoleMenu;
import com.ydl.shopping.exception.ServiceException;
import com.ydl.shopping.mapper.TbRoleMapper;
import com.ydl.shopping.service.ITbRoleMenuService;
import com.ydl.shopping.service.ITbRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ydl.shopping.constant.RedisConstants.ROLE_KEY;

@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole> implements ITbRoleService {
    @Resource
    private TbRoleMapper tbRoleMapper;

    @Resource
    private ITbRoleMenuService tbRoleMenuService;

    @Override
    public List<TbRole> selectList(TbRole sysRole) {
        return tbRoleMapper.queryList(sysRole);
    }

    @Override
    public String saveRole(TbRole sysRole) {
        String prefix = sysRole.getPerms().substring(0, 5);
        if (!prefix.equals("ROLE_")) {
            sysRole.setPerms("ROLE_" + sysRole.getPerms());
        }
        boolean result = save(sysRole);
        if (result) {
            Long roleId = sysRole.getId();
            if (sysRole.getIsDefault().equals(1)) {
                boolean update = update().set("is_default", 0).ne("id", roleId).update();
                if (!update) {
                    throw new ServiceException(ResultEnum.ERROR);
                }
            }
            List<Long> menuIds = sysRole.getMenuIds();
            String saveResult = tbRoleMenuService.saveRoleMenu(roleId, menuIds);
            if (StrUtil.isNotBlank(saveResult)) {
                throw new ServiceException(ResultEnum.ERROR);
            }
            return null;
        }
        return "操作失败";
    }

    @Override
    public void saveSuperAdmin(Long menuId) {
        tbRoleMenuService.save(new TbRoleMenu(1L, menuId));
    }

    @Override
    public TbRole getDefaultRole() {
        return query().eq("is_default", 1).one();
    }

    @Override
    public TbRole getRoleByPerms(String perms) {
        if (StrUtil.isBlank(perms)) {
            return null;
        }
        return query().eq("perms", perms).one();
    }

    @Override
    public List<TreeSelect> select() {
        QueryWrapper<TbRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", 1);

        List<TbRole> tbRoles = list(queryWrapper);
        List<TreeSelect> treeSelects = new ArrayList<>();

        for (TbRole tbRole : tbRoles) {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(tbRole.getId());
            treeSelect.setLabel(tbRole.getName());
            treeSelect.setValue(tbRole.getId().toString());
            treeSelects.add(treeSelect);
        }

        return treeSelects;
    }
}
